<template>
    <el-form :model="formData" :rules="rules2" ref="formData" label-position="left" label-width="0px" style="background-image(./img/bk1.jpg)"
             class="demo-ruleForm login-container">
        <h3 class="title">用户登录</h3>
        <el-form-item prop="account">
            <el-input type="text" v-model="formData.account"  prefix-icon="fa fa-user"  auto-complete="on" placeholder="账号"></el-input>
        </el-form-item>
        <el-form-item prop="checkPass">
            <el-input :type="pwdType" v-model="formData.checkPass" prefix-icon="fa fa-lock" auto-complete="off" placeholder="密码"></el-input>
            <span class="show-pwd" @click="showPwd"><i class="fa fa-eye" aria-hidden="true"></i></span>
        </el-form-item>
        <el-checkbox v-model="checked" checked class="remember">记住密码</el-checkbox>
        <el-button @click.native.prevent="handleReset" style="float:right;">重置</el-button>
        <el-form-item style="width:100%;">
            <el-button type="primary" style="width:100%;" @click.native.prevent="handleSubmit" :loading="loginLoading">登录
            </el-button>
        </el-form-item>
    </el-form>
</template>

<script>
    import * as types from '../store/types'
    import _CONSTANT from '../common/constant'
    export default {
        data() {
            return {
                formData: {
                    account: '',
                    checkPass: ''
                },

                loginLoading:false,
                rules2: { //字段校验
                    account: [
                        {required: true, message: '请输入账号', trigger: 'blur'},
                        //{ validator: validatePass }
                    ],
                    checkPass: [
                        {required: true, message: '请输入密码', trigger: 'blur'},
                        //{ validator: validatePass2 }
                    ]
                },
                checked: true,
                pwdType: 'password'
            };
        },
        methods: {
            handleReset() {
                this.$refs.formData.resetFields();
            },
            handleSubmit(ev) {

                var _this = this;

                this.$refs.formData.validate((valid) => {

                    if (valid) {
                       this.toLogin();
                    } else {
                        return false;
                    }
                });
            },
            showPwd() {
                if (this.pwdType === 'password') {
                    this.pwdType = ''
                } else {
                    this.pwdType = 'password'
                }
            },
            toLogin(){

                this.loginLoading=true;

                let loginParams = {username: this.formData.account, password: this.formData.checkPass,rememberMe:this.checked};

                this.$http.post('/api/toLogin', loginParams).then(res => {

                    this.loginLoading=false;

                    if (res.data.code) {

                        this.$message({
                            message:res.data.msg ,//"用户名或密码错误",
                            type: 'error'
                        });

                    } else {
                        localStorage.setItem("CurrentUser",JSON.stringify(res.data.user));
                        this.$store.commit(types.LOGIN, res.data.token); //保存token至store
                        if(res.data.user.usertype==_CONSTANT.SYSTEM){  //系统管理员
                             this.$router.push({path: '/stuWelcome'});
                        }
                        else if(res.data.user.usertype==_CONSTANT.STUDENT) { //学生
                             this.$router.push({path: '/welcome'}); //路由跳转
                         }
                        else if (res.data.user.usertype==_CONSTANT.TEACHER) {   //教师
                            this.$router.push({path: '/personal'}); //路由跳转
                       }

                       /* this.$router.push({path: '/personal'});*/
                        this.$message({
                            message: '恭喜你，登录成功',
                            type: 'success'
                        });
                    }
                }).catch((error) => {
                    this.loginLoading=false;
                    if(error) console.log(error);
                });

            }
        }
    }

</script>

<style lang="scss" scoped>
    .login-container {
        /*box-shadow: 0 0px 8px 0 rgba(0, 0, 0, 0.06), 0 1px 0px 0 rgba(0, 0, 0, 0.02);*/
        -webkit-border-radius: 5px;
        border-radius: 5px;
        -moz-border-radius: 5px;
        background-clip: padding-box;
        margin: 180px auto;
        width: 350px;
        padding: 35px 35px 15px 35px;
        background: #fff;
        border: 1px solid #eaeaea;
        box-shadow: 0 0 25px #cac6c6;
        .title {
            margin: 0px auto 40px auto;
            text-align: center;
            color: #505458;
        }
        .remember {
            margin: 0px 0px 35px 0px;
        }
    }

    .show-pwd {
        position: absolute;
        right: 10px;
        top: 0px;
        font-size: 16px;
        cursor: pointer;
        color: #909399;
    }
</style>
